package com.butuh.uang.tuh.ang.ui;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.butuh.uang.tuh.ang.R;
import com.butuh.uang.tuh.ang.adapter.DownHistoryAdapter;
import com.butuh.uang.tuh.ang.model.EventDialog;
import com.butuh.uang.tuh.ang.model.HomeListBean;
import com.butuh.uang.tuh.ang.present.PDownloadHistory;
import com.butuh.uang.tuh.ang.util.DownLoadUtil;
import com.butuh.uang.tuh.ang.util.FaceBookShareUtils;
import com.butuh.uang.tuh.ang.util.OnClickLogUtils;
import com.butuh.uang.tuh.ang.util.ShareUtil;
import com.butuh.uang.tuh.ang.util.ShatsAppShareUtils;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.lanren.easydialog.DialogViewHolder;
import com.lanren.easydialog.EasyDialog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.droidlover.xdroidmvp.log.XLog;
import cn.droidlover.xdroidmvp.mvp.XActivity;

public class DownHistoryActivity extends XActivity<PDownloadHistory> implements AdapterView.OnItemClickListener, FacebookCallback {
    @BindView(R.id.lv_history)
    ListView mLvHistory;

    DownHistoryAdapter downHistoryAdapter;

    @BindView(R.id.layout_no_net)
    LinearLayout mLayoutNoNet;
    @BindView(R.id.layout_error)
    LinearLayout mLayoutError;
    @BindView(R.id.tv_error_message)
    TextView mTvErrorMessage;

    String yemianName = "DownloadHistory";
    @Override
    public void initData(Bundle savedInstanceState) {
        EventBus.getDefault().register(this);
        OnClickLogUtils.onClickLog(this, "come-in-into", "[\\\"DownloadHistory\\\"]", 0);
        mLvHistory.setOnItemClickListener(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGetMessage(EventDialog message) {
        mDialog.Dismiss();
    }

    @Override
    protected void onResume() {
        super.onResume();
        downHistoryAdapter = new DownHistoryAdapter(this);
        mLvHistory.setAdapter(downHistoryAdapter);
        mDialog.Show();
        getP().getHistoryList();
    }


    public void loadList(List<HomeListBean> list) {
        mDialog.Dismiss();
        if (list.size() != 0) {
            mLvHistory.setVisibility(View.VISIBLE);
            mLayoutNoNet.setVisibility(View.GONE);
            mLayoutError.setVisibility(View.GONE);
            downHistoryAdapter.setData(list);
        } else {
            mLvHistory.setVisibility(View.GONE);
            mLayoutNoNet.setVisibility(View.GONE);
            mLayoutError.setVisibility(View.VISIBLE);
            mTvErrorMessage.setText("Saat ini tidak ada produk");
        }
    }


    public void loadListError(int type, String message) {
        mDialog.dismiss();
        if (type == 1) {
            mLvHistory.setVisibility(View.GONE);
            mLayoutNoNet.setVisibility(View.VISIBLE);
            mLayoutError.setVisibility(View.GONE);
        } else {
            mLvHistory.setVisibility(View.GONE);
            mLayoutNoNet.setVisibility(View.GONE);
            mLayoutError.setVisibility(View.VISIBLE);
            mTvErrorMessage.setText(message);
        }
    }


    @OnClick({R.id.iv_back, R.id.iv_share})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                OnClickLogUtils.onClickPermute(context, yemianName, "back", 0);
                finish();
                break;
            case R.id.iv_share:
                OnClickLogUtils.onClickPermute(context, yemianName, "share", 0);
                ShareDialog();
                break;
        }
    }

    FaceBookShareUtils faceBookShareUtils = null;

    CallbackManager callBackManager = null;


    private void ShareDialog() {
        new EasyDialog(this, R.layout.dialog_share) {//设置自己的自定义布局文件
            @Override
            public void onBindViewHolder(DialogViewHolder holder) {
                holder.setOnClick(R.id.iv_share_close, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dismiss();

                    }
                });
                holder.setOnClick(R.id.iv_share_facebook, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dismiss();
                        if (null == callBackManager) {
                            callBackManager = CallbackManager.Factory.create();
                        }
                        if (null == faceBookShareUtils) {
                            faceBookShareUtils = new FaceBookShareUtils(DownHistoryActivity.this, callBackManager, DownHistoryActivity.this);
                        }
                        faceBookShareUtils.share(ShareUtil.SHARE_NAME,ShareUtil.SHARE_URL);
                        OnClickLogUtils.onClickPermute(context, yemianName, "shareFaceBook", 0);
                    }
                });
                holder.setOnClick(R.id.iv_share_whatsapp, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dismiss();
                        ShatsAppShareUtils.share(DownHistoryActivity.this, ShareUtil.SHARE_URL);
                        OnClickLogUtils.onClickPermute(context, yemianName, "shareWhatsApp", 0);
                    }
                });
            }
        }.backgroundLight(0.2)
                .setCanceledOnTouchOutside(false)
                .setCancelAble(false)
                .showDialog();
    }


    @Override
    public int getLayoutId() {
        return R.layout.activity_down_history;
    }

    @Override
    public PDownloadHistory newP() {
        return new PDownloadHistory();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        mDialog.Show();
        HomeListBean bean = downHistoryAdapter.getDataSource().get(position);
        DownLoadUtil.download(this, "downhistory", "historylist", position, bean.getBianhao(), bean.getPattern(), bean.getOpenMode(), bean.getSite(), bean.getBag());
    }

    @Override
    public void onSuccess(Object o) {
        XLog.e("------分享成功-----" + o.toString());
    }

    @Override
    public void onCancel() {
        XLog.e("------关闭分享-----");
    }

    @Override
    public void onError(FacebookException error) {
        XLog.e("------分享onError-----");
    }
}
